﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Wjtour.Common;
using Hjp.FrameWork.Common;
using Wjtour.BLL;
using System.Linq;
using TongCheng.API.OpenAPI.Request;
using TongCheng.API.OpenAPI.Facade;
using TongCheng.API.OpenAPI.Domain;

namespace Wjtour.Web.Front
{
    public partial class ticketview : BasePage
    {
        #region 固定分页参数
        protected int page = 1;
        protected int pagesize = 4;
        #endregion

        scene_bll _bll = new scene_bll();
        protected PaginationData<Wjtour.Domain.scene> data = new PaginationData<Domain.scene>();
        protected int type = 1;
        protected List<Wjtour.Domain.line> recommendLines = new List<Domain.line>();
        Wjtour.BLL.line_bll _linebll = new line_bll();
        protected List<Wjtour.Domain.article> trafficGuides = new List<Domain.article>();//旅游攻略
        Wjtour.BLL.article_bll _articleBll = new article_bll();
        protected int id = 0;
        protected TongCheng.API.OpenAPI.Domain.Scenery scenery = new TongCheng.API.OpenAPI.Domain.Scenery();

        protected TextBox txtComeDate;
        protected TextBox txtLeaveDate;
        protected TextBox txtLocation;
        protected TextBox txtHotelName;
        protected TextBox txtSceneryName;
        protected Button hotelSearch, scenerySearch;

        protected void Page_Load(object sender, EventArgs e)
        {
            page = Utils.StrToInt(Request["page"], 1);
            id = Utils.StrToInt(Request["id"], 1);
            hotelSearch.Click += hotelSearch_Click;
            scenerySearch.Click += scenerySearch_Click;

            bindList();

            if (id > 0)
            {
                getSceneryDetail(id);
            }
        }

        void scenerySearch_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(txtSceneryName.Text))
            {
                string query = "s=" + HttpUtility.UrlEncode(txtSceneryName.Text);
                Response.Redirect("ticketlist.aspx?" + query);
            }
            Response.Redirect("ticketlist.aspx");
        }

        void hotelSearch_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(txtComeDate.Text))
            {
                Utils.Alert(this.Page, "请选择入住日期！");
                return;
            }
            if (string.IsNullOrEmpty(txtLeaveDate.Text))
            {
                Utils.Alert(this.Page, "请选择离店日期！");
                return;
            }
            DateTime dt1 = DateTime.Now;
            DateTime dt2 = DateTime.Now.AddDays(1);
            DateTime.TryParse(txtComeDate.Text, out dt1);
            DateTime.TryParse(txtLeaveDate.Text, out dt1);
            if (dt1 > dt2)
            {
                Utils.Alert(this.Page, "入住日期不能晚于离店日期！");
                return;
            }

            string query = string.Format("cd={0}&ld={1}&loc={2}&n={3}", txtComeDate.Text, txtLeaveDate.Text, txtLocation.Text, txtHotelName.Text);
            Response.Redirect("hotellist.aspx?" + query);
        }

        void getSceneryDetail(int id)
        {
            GetSceneryDetailRequest request = new GetSceneryDetailRequest();
            request.Body.ClientIp = "172.0.0.1";
            request.Body.Cs = "1";
            request.Body.SceneryId = id;

            var response = SceneryFacade.GetSceneryDetail(request);
            scenery = response.Body.scenery;

            GetSceneryPriceRequest request2 = new GetSceneryPriceRequest();
            request2.Body.ClientIp = "172.0.0.1";
            request2.Body.SceneryIds = id.ToString();
            request2.Body.ShowDetail = 2;

            var response2 = SceneryFacade.GetSceneryPrice(request2);
            var sceneryList = response2.Body.sceneryList.Items;
            for (int i = 0; i < sceneryList.Count; i++)
            {
                if (sceneryList[i].sceneryId == scenery.sceneryId)
                {
                    scenery.PolicyList = sceneryList[i].PolicyList;
                }
            }


            GetSceneryListRequest request3 = new GetSceneryListRequest();
            request3.Body.ClientIp = "172.0.0.1";
            request3.Body.ProvinceId = 16;
            request3.Body.CityId = 226;
            request3.Body.Page = 1;
            request3.Body.Cs = "1";
            request3.Body.SortType = 0;
            request3.Body.GradeId = "1,2,3,4,5";
            request3.Body.PageSize = 100;

            var response3 = SceneryFacade.GetSceneryList(request3);
            var sl = response3.Body.sceneryList;
            var list2 = response3.Body.sceneryList.Items;
            for (int i = 0; i < list2.Count; i++)
            {
                if (list2[i].sceneryId == scenery.sceneryId)
                {
                    scenery.imgPath = sl.ImgbaseURL + list2[i].imgPath;
                }
            }

            GetSceneryTrafficInfoRequest request4 = new GetSceneryTrafficInfoRequest();
            request4.Body.ClientIp = "172.0.0.1";
            request4.Body.SceneryId = id;

            var response4 = SceneryFacade.GetSceneryTrafficInfo(request4);
            scenery.traffic = response4.Body.scenery.traffic;
        }

        void bindList()
        {
            data = _bll.GetList(null, type.ToString(), page - 1, pagesize);
            PagnationHtml = GetPageHtml("sceniclist.aspx", page, data.Pager.TotalPages);

            recommendLines = _linebll.GetAll<Wjtour.Domain.line>("select top 6 * from line where isrecommend=1").ToList();
            trafficGuides = _articleBll.GetAuditTopListWithFirstLogo("6", 7, 2);
        }
    }
}
